<!DOCTYPE html>
<html lang="en-us"><head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'><meta name='description' content='Requirements: Note: Programming in C, Let n be a positive integer with specified initial value n=30. Use an array of n units to store polynomials of one indeterminate (一元多项式). Design and implement a C program to:
 Determine the data structure of the array Use a stack S in the array to store unused units. Initially all the units are unused and the 0 to (n-1) indexes spread randomly on the stack Use two lists LP and LQ in the array to store two polynomials P and Q of one indeterminate Show the map of the array Adding P and Q, restore the result as a list LPQ in the array, and release all garbage units to the stack Show the map of the array again  Solution： ​	链表的静态实现法，利用数组模拟动态链表，用栈来标记还未使用的节点。'><title>Polynomials of one indeterminate</title>
    
    <link rel='canonical' href='https://hungyam.gitee.io/code/p/polynomials-of-one-indeterminate/'>

<link rel="stylesheet" href="/code/scss/style.min.css"><meta property='og:title' content='Polynomials of one indeterminate'>
<meta property='og:description' content='Requirements: Note: Programming in C, Let n be a positive integer with specified initial value n=30. Use an array of n units to store polynomials of one indeterminate (一元多项式). Design and implement a C program to:
 Determine the data structure of the array Use a stack S in the array to store unused units. Initially all the units are unused and the 0 to (n-1) indexes spread randomly on the stack Use two lists LP and LQ in the array to store two polynomials P and Q of one indeterminate Show the map of the array Adding P and Q, restore the result as a list LPQ in the array, and release all garbage units to the stack Show the map of the array again  Solution： ​	链表的静态实现法，利用数组模拟动态链表，用栈来标记还未使用的节点。'>
<meta property='og:url' content='https://hungyam.gitee.io/code/p/polynomials-of-one-indeterminate/'>
<meta property='og:site_name' content='Hungyam'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:published_time' content='2020-10-13T00:00:00&#43;00:00'/><meta property='article:modified_time' content='2020-10-13T00:00:00&#43;00:00'/><meta property='og:image' content='https://hungyam.gitee.io/code/p/polynomials-of-one-indeterminate/1.jpg' />
<meta name="twitter:title" content="Polynomials of one indeterminate">
<meta name="twitter:description" content="Requirements: Note: Programming in C, Let n be a positive integer with specified initial value n=30. Use an array of n units to store polynomials of one indeterminate (一元多项式). Design and implement a C program to:
 Determine the data structure of the array Use a stack S in the array to store unused units. Initially all the units are unused and the 0 to (n-1) indexes spread randomly on the stack Use two lists LP and LQ in the array to store two polynomials P and Q of one indeterminate Show the map of the array Adding P and Q, restore the result as a list LPQ in the array, and release all garbage units to the stack Show the map of the array again  Solution： ​	链表的静态实现法，利用数组模拟动态链表，用栈来标记还未使用的节点。"><meta name="twitter:card" content="summary_large_image">
	<meta name="twitter:image" content='https://hungyam.gitee.io/code/p/polynomials-of-one-indeterminate/1.jpg' /></head><body class="">
        <div class="container flex on-phone--column align-items--flex-start extended article-page with-toolbar">
            <aside class="sidebar left-sidebar sticky">
    <button class="hamburger hamburger--spin" type="button" id="toggle-menu" aria-label="">
        <span class="hamburger-box">
            <span class="hamburger-inner"></span>
        </span>
    </button>

    <header class="site-info">
        
            <figure class="site-avatar">
                

                
                    
                    <img src="/code/img/avatar_hu271a79c54c9badde22d864b5ad68a303_161614_300x300_resize_q75_box.jpg" width="300"
                        height="300" class="site-logo" loading="lazy" alt="Avatar">
                

                <span class="emoji">🐤</span>
            </figure>
        
        <h1 class="site-name"><a href="https://hungyam.gitee.io/code">Hungyam</a></h1>
        <h2 class="site-description">Programming enthusiast</h2>
    </header>

    <ol class="menu" id="main-menu">
        
        
        

        <li >
            <a href='https://hungyam.gitee.io/code/'>
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-home" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="5 12 3 12 12 3 21 12 19 12" />
  <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
</svg>



                
                <span>Home</span>
            </a>
        </li>
        
        

        <li >
            <a href='https://hungyam.gitee.io/code/about'>
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="7" r="4" />
  <path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2" />
</svg>



                
                <span>About me</span>
            </a>
        </li>
        
        

        <li >
            <a href='https://hungyam.gitee.io/code/archives'>
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <rect x="3" y="4" width="18" height="4" rx="2" />
  <path d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10" />
  <line x1="10" y1="12" x2="14" y2="12" />
</svg>



                
                <span>Archives</span>
            </a>
        </li>
        
    </ol>
</aside>
            <main class="main full-width">
    <div id="article-toolbar">
        <a href="https://hungyam.gitee.io/code" class="back-home">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="15 6 9 12 15 18" />
</svg>



            <span>Back</span>
        </a>
    </div>

    <article class="has-image main-article">
    <header class="article-header">
        <div class="article-image">
            <img srcset="/code/p/polynomials-of-one-indeterminate/1_hud7e36f7e20e71be184458283bdae4646_55974_1024x0_resize_q75_box.jpg 1024w, /code/p/polynomials-of-one-indeterminate/1_hud7e36f7e20e71be184458283bdae4646_55974_2000x0_resize_q75_box.jpg 2000w"
                    src="/code/p/polynomials-of-one-indeterminate/1_hud7e36f7e20e71be184458283bdae4646_55974_2000x0_resize_q75_box.jpg" width="1000" height="667" loading="lazy"
                    alt="Featured image of post Polynomials of one indeterminate" />
            
        </div>
    

    <div class="article-details">
    
    <header class="article-category">
        
            <a href="https://hungyam.gitee.io/code/categories/c/" 
                    class="color-tag"
                    data-image="/code/p/polynomials-of-one-indeterminate/1_hud7e36f7e20e71be184458283bdae4646_55974_20x20_fill_q75_box_smart1.jpg" 
                    data-key="" 
                    data-hash="md5-xRTZFpFxc6SKQuARS0aZYQ==">
                    C
                </a>
            
        
            <a href="https://hungyam.gitee.io/code/categories/stack/" 
                    class="color-tag"
                    data-image="/code/p/polynomials-of-one-indeterminate/1_hud7e36f7e20e71be184458283bdae4646_55974_20x20_fill_q75_box_smart1.jpg" 
                    data-key="" 
                    data-hash="md5-xRTZFpFxc6SKQuARS0aZYQ==">
                    Stack
                </a>
            
        
    </header>
    

    <h2 class="article-title">
        <a href="https://hungyam.gitee.io/code/p/polynomials-of-one-indeterminate/">Polynomials of one indeterminate</a>
    </h2>

    <footer class="article-time">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



        <time class="article-time--published">2020 Oct 13</time>
    </footer></div>
</header>

    <section class="article-content">
    <h1 id="requirements">Requirements:</h1>
<p>Note: Programming in C,
Let n be a positive integer with specified initial value n=30. Use an array of n units to store polynomials of one indeterminate (一元多项式). Design and implement a C program to:</p>
<ol>
<li>Determine the data structure of the array</li>
<li>Use a stack S in the array to store unused units. Initially all the units are unused and the 0 to
(n-1) indexes spread randomly on the stack</li>
<li>Use two lists LP and LQ in the array to store two polynomials P and Q of one indeterminate</li>
<li>Show the map of the array</li>
<li>Adding P and Q, restore the result as a list LPQ in the array, and release all garbage units to the stack</li>
<li>Show the map of the array again</li>
</ol>
<h2 id="solution">Solution：</h2>
<p>​	链表的静态实现法，利用数组模拟动态链表，用栈来标记还未使用的节点。</p>
<h4 id="节点的声明">节点的声明</h4>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="k">typedef</span> <span class="k">struct</span> <span class="n">node</span><span class="p">{</span>        <span class="c1">//the list node
</span><span class="c1"></span>    <span class="kt">int</span> <span class="n">data</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">highpower</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">next</span><span class="p">;</span>
<span class="p">}</span><span class="n">Node</span><span class="p">;</span>
</code></pre></div><h4 id="一些变量的声明">一些变量的声明</h4>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="n">Node</span> <span class="n">list</span><span class="p">[</span><span class="mi">30</span><span class="p">]</span><span class="o">=</span><span class="p">{</span><span class="mi">0</span><span class="p">};</span>          <span class="c1">//List_array
</span><span class="c1"></span><span class="kt">int</span> <span class="n">head_p</span><span class="o">=-</span><span class="mi">1</span><span class="p">;</span>              <span class="c1">//Head of list LP
</span><span class="c1"></span><span class="kt">int</span> <span class="n">head_q</span><span class="o">=-</span><span class="mi">1</span><span class="p">;</span>              <span class="c1">//Head of list LQ
</span><span class="c1"></span><span class="kt">int</span> <span class="n">head_sum</span><span class="o">=-</span><span class="mi">1</span><span class="p">;</span>            <span class="c1">//Head of list LPQ
</span><span class="c1"></span><span class="kt">int</span> <span class="n">manage_stack</span><span class="p">[</span><span class="mi">30</span><span class="p">]</span><span class="o">=</span><span class="p">{</span><span class="mi">7</span><span class="p">,</span><span class="mi">24</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">16</span><span class="p">,</span><span class="mi">22</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">18</span><span class="p">,</span><span class="mi">27</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">23</span><span class="p">,</span><span class="mi">25</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">13</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">21</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">11</span><span class="p">,</span><span class="mi">15</span><span class="p">,</span><span class="mi">26</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">19</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">28</span><span class="p">,</span><span class="mi">29</span><span class="p">,</span><span class="mi">12</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">14</span><span class="p">};</span> <span class="c1">//Store unused units
</span><span class="c1"></span><span class="kt">int</span> <span class="n">top</span><span class="o">=</span><span class="mi">29</span><span class="p">;</span>                 <span class="c1">//Top of stack
</span></code></pre></div><h4 id="一元多项式的打印">一元多项式的打印</h4>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="kt">void</span> <span class="nf">print_list</span><span class="p">(</span><span class="kt">int</span> <span class="n">head</span><span class="p">){</span>          <span class="c1">//Print the polynomials of one indeterminate
</span><span class="c1"></span>    <span class="k">while</span> <span class="p">(</span><span class="n">head</span><span class="o">!=-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">highpower</span><span class="o">&gt;</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&#34;%dx^%d&#34;</span><span class="p">,</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">data</span><span class="p">,</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">highpower</span><span class="p">);</span>
        <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">highpower</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&#34;%dx&#34;</span><span class="p">,</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">data</span><span class="p">);</span>
        <span class="k">else</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&#34;%d&#34;</span><span class="p">,</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">data</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">next</span><span class="o">==-</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
        <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">next</span><span class="p">].</span><span class="n">data</span><span class="o">&gt;=</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&#34;+&#34;</span><span class="p">);</span>
        <span class="n">head</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><h4 id="储存空间的打印">储存空间的打印</h4>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="kt">void</span> <span class="nf">print_array</span><span class="p">(</span><span class="kt">int</span> <span class="n">list1</span><span class="p">){</span>        <span class="c1">//Show the map of the array
</span><span class="c1"></span>    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">index</span><span class="se">\t</span><span class="s">data</span><span class="se">\t</span><span class="s">highpower</span><span class="se">\t</span><span class="s">next</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">temp</span><span class="o">=</span><span class="n">list1</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">temp</span><span class="o">!=-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&#34;%d</span><span class="se">\t\t</span><span class="s">%d</span><span class="se">\t\t</span><span class="s">%d</span><span class="se">\t\t\t</span><span class="s">%d</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">temp</span><span class="p">,</span><span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">data</span><span class="p">,</span><span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">highpower</span><span class="p">,</span><span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">next</span><span class="p">);</span>
        <span class="n">temp</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><h4 id="空间管理栈的打印">空间管理栈的打印</h4>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="kt">void</span> <span class="nf">print_stack</span><span class="p">(){</span>                 <span class="c1">//Show the map of the stack
</span><span class="c1"></span>    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">Stack(from bottom to top):&#34;</span><span class="p">);</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;=</span><span class="n">top</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&#34;%d &#34;</span><span class="p">,</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
    <span class="p">}</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><h4 id="一元多项式加法的实现">一元多项式加法的实现</h4>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="kt">void</span> <span class="nf">add</span><span class="p">(){</span>                                 <span class="c1">//Adding list P and Q
</span><span class="c1"></span>    <span class="kt">int</span> <span class="n">temp</span><span class="o">=-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">head_p</span><span class="o">==-</span><span class="mi">1</span><span class="o">||</span><span class="n">head_q</span><span class="o">==-</span><span class="mi">1</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">head_sum</span><span class="o">==-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>                     <span class="c1">//Determines whether it is the first node
</span><span class="c1"></span>            <span class="n">head_sum</span><span class="o">=</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="o">--</span><span class="p">];</span>
            <span class="n">temp</span><span class="o">=</span><span class="n">head_sum</span><span class="p">;</span>
        <span class="p">}</span><span class="k">else</span><span class="p">{</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">next</span><span class="o">=</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="o">--</span><span class="p">];</span>
            <span class="n">temp</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">highpower</span><span class="o">&gt;</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">highpower</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">data</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">data</span><span class="p">;</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">highpower</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">highpower</span><span class="p">;</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">next</span><span class="o">=-</span><span class="mi">1</span><span class="p">;</span>
            <span class="n">manage_stack</span><span class="p">[</span><span class="o">++</span><span class="n">top</span><span class="p">]</span><span class="o">=</span><span class="n">head_p</span><span class="p">;</span>
            <span class="n">head_p</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
        <span class="p">}</span><span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">highpower</span><span class="o">&gt;</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">highpower</span><span class="p">){</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">data</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">data</span><span class="p">;</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">highpower</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">highpower</span><span class="p">;</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">next</span><span class="o">=-</span><span class="mi">1</span><span class="p">;</span>
            <span class="n">manage_stack</span><span class="p">[</span><span class="o">++</span><span class="n">top</span><span class="p">]</span><span class="o">=</span><span class="n">head_q</span><span class="p">;</span>
            <span class="n">head_q</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
        <span class="p">}</span><span class="k">else</span><span class="p">{</span>
            <span class="k">if</span><span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">data</span><span class="o">+</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">data</span><span class="o">==</span><span class="mi">0</span><span class="p">){</span>
                <span class="n">manage_stack</span><span class="p">[</span><span class="o">++</span><span class="n">top</span><span class="p">]</span><span class="o">=</span><span class="n">head_p</span><span class="p">;</span>
                <span class="n">head_p</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
                <span class="n">manage_stack</span><span class="p">[</span><span class="o">++</span><span class="n">top</span><span class="p">]</span><span class="o">=</span><span class="n">head_q</span><span class="p">;</span>
                <span class="n">head_q</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
                <span class="n">manage_stack</span><span class="p">[</span><span class="o">++</span><span class="n">top</span><span class="p">]</span><span class="o">=</span><span class="n">temp</span><span class="p">;</span>
                <span class="k">continue</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">data</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">data</span><span class="o">+</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">data</span><span class="p">;</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">highpower</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">highpower</span><span class="p">;</span>
            <span class="n">list</span><span class="p">[</span><span class="n">temp</span><span class="p">].</span><span class="n">next</span><span class="o">=-</span><span class="mi">1</span><span class="p">;</span>
            <span class="n">manage_stack</span><span class="p">[</span><span class="o">++</span><span class="n">top</span><span class="p">]</span><span class="o">=</span><span class="n">head_p</span><span class="p">;</span>
            <span class="n">head_p</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_p</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
            <span class="n">manage_stack</span><span class="p">[</span><span class="o">++</span><span class="n">top</span><span class="p">]</span><span class="o">=</span><span class="n">head_q</span><span class="p">;</span>
            <span class="n">head_q</span><span class="o">=</span><span class="n">list</span><span class="p">[</span><span class="n">head_q</span><span class="p">].</span><span class="n">next</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><h3 id="main函数">main函数</h3>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">p_num</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">q_num</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">data</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="n">scanf</span><span class="p">(</span><span class="s">&#34;%d&#34;</span><span class="p">,</span><span class="o">&amp;</span><span class="n">p_num</span><span class="p">);</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">p_num</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>               <span class="c1">//Input list P
</span><span class="c1"></span>        <span class="n">scanf</span><span class="p">(</span><span class="s">&#34;%d&#34;</span><span class="p">,</span><span class="o">&amp;</span><span class="n">data</span><span class="p">);</span>
        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">data</span><span class="p">)</span>
            <span class="k">continue</span><span class="p">;</span>
        <span class="n">list</span><span class="p">[</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="p">]].</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">;</span>
        <span class="n">list</span><span class="p">[</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="p">]].</span><span class="n">highpower</span><span class="o">=</span><span class="n">i</span><span class="p">;</span>
        <span class="n">list</span><span class="p">[</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="p">]].</span><span class="n">next</span><span class="o">=</span><span class="n">head_p</span><span class="p">;</span>
        <span class="n">head_p</span><span class="o">=</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="o">--</span><span class="p">];</span>
    <span class="p">}</span>
    <span class="n">scanf</span><span class="p">(</span><span class="s">&#34;%d&#34;</span><span class="p">,</span><span class="o">&amp;</span><span class="n">q_num</span><span class="p">);</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">q_num</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>                 <span class="c1">//Input list Q
</span><span class="c1"></span>        <span class="n">scanf</span><span class="p">(</span><span class="s">&#34;%d&#34;</span><span class="p">,</span><span class="o">&amp;</span><span class="n">data</span><span class="p">);</span>
        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">data</span><span class="p">)</span>
            <span class="k">continue</span><span class="p">;</span>
        <span class="n">list</span><span class="p">[</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="p">]].</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">;</span>
        <span class="n">list</span><span class="p">[</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="p">]].</span><span class="n">highpower</span><span class="o">=</span><span class="n">i</span><span class="p">;</span>
        <span class="n">list</span><span class="p">[</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="p">]].</span><span class="n">next</span><span class="o">=</span><span class="n">head_q</span><span class="p">;</span>
        <span class="n">head_q</span><span class="o">=</span><span class="n">manage_stack</span><span class="p">[</span><span class="n">top</span><span class="o">--</span><span class="p">];</span>
    <span class="p">}</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">*****  the first polynomials of one indeterminate  *****</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
    <span class="n">print_list</span><span class="p">(</span><span class="n">head_p</span><span class="p">);</span>
    <span class="n">print_array</span><span class="p">(</span><span class="n">head_p</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">*****  the second polynomials of one indeterminate  *****</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
    <span class="n">print_list</span><span class="p">(</span><span class="n">head_q</span><span class="p">);</span>
    <span class="n">print_array</span><span class="p">(</span><span class="n">head_q</span><span class="p">);</span>
    <span class="n">print_stack</span><span class="p">();</span>
    <span class="n">add</span><span class="p">();</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">*****  the sum polynomials of one indeterminate  *****</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
    <span class="n">print_list</span><span class="p">(</span><span class="n">head_sum</span><span class="p">);</span>
    <span class="n">print_array</span><span class="p">(</span><span class="n">head_sum</span><span class="p">);</span>
    <span class="n">print_stack</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h2 id="xcode运行结果">Xcode运行结果</h2>
<div class="highlight"><pre class="chroma"><code class="language-C" data-lang="C"><span class="nl">input</span><span class="p">:</span>
<span class="mi">4</span>
<span class="mi">1</span> <span class="mi">3</span> <span class="mi">4</span> <span class="mi">2</span>
<span class="mi">5</span>
<span class="mi">2</span> <span class="o">-</span><span class="mi">3</span> <span class="mi">0</span> <span class="mi">2</span> <span class="o">-</span><span class="mi">1</span>

<span class="nl">output</span><span class="p">:</span>
<span class="o">*****</span>  <span class="n">the</span> <span class="n">first</span> <span class="n">polynomials</span> <span class="n">of</span> <span class="n">one</span> <span class="n">indeterminate</span>  <span class="o">*****</span>
<span class="mi">2</span><span class="n">x</span><span class="o">^</span><span class="mi">3</span><span class="o">+</span><span class="mi">4</span><span class="n">x</span><span class="o">^</span><span class="mi">2</span><span class="o">+</span><span class="mi">3</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span>

<span class="n">index</span>	<span class="n">data</span>	<span class="n">highpower</span>	<span class="n">next</span>
<span class="mi">29</span>		<span class="mi">2</span>		<span class="mi">3</span>			<span class="mi">12</span>
<span class="mi">12</span>		<span class="mi">4</span>		<span class="mi">2</span>			<span class="mi">2</span>
<span class="mi">2</span>		<span class="mi">3</span>		<span class="mi">1</span>			<span class="mi">14</span>
<span class="mi">14</span>		<span class="mi">1</span>		<span class="mi">0</span>			<span class="o">-</span><span class="mi">1</span>

<span class="o">*****</span>  <span class="n">the</span> <span class="n">second</span> <span class="n">polynomials</span> <span class="n">of</span> <span class="n">one</span> <span class="n">indeterminate</span>  <span class="o">*****</span>
<span class="o">-</span><span class="mi">1</span><span class="n">x</span><span class="o">^</span><span class="mi">4</span><span class="o">+</span><span class="mi">2</span><span class="n">x</span><span class="o">^</span><span class="mi">3</span><span class="o">-</span><span class="mi">3</span><span class="n">x</span><span class="o">+</span><span class="mi">2</span>

<span class="n">index</span>	<span class="n">data</span>	<span class="n">highpower</span>	<span class="n">next</span>
<span class="mi">5</span>		<span class="o">-</span><span class="mi">1</span>		<span class="mi">4</span>			<span class="mi">19</span>
<span class="mi">19</span>		<span class="mi">2</span>		<span class="mi">3</span>			<span class="mi">10</span>
<span class="mi">10</span>		<span class="o">-</span><span class="mi">3</span>		<span class="mi">1</span>			<span class="mi">28</span>
<span class="mi">28</span>		<span class="mi">2</span>		<span class="mi">0</span>			<span class="o">-</span><span class="mi">1</span>

<span class="n">Stack</span><span class="p">(</span><span class="n">from</span> <span class="n">bottom</span> <span class="n">to</span> <span class="n">top</span><span class="p">)</span><span class="o">:</span><span class="mi">7</span> <span class="mi">24</span> <span class="mi">9</span> <span class="mi">20</span> <span class="mi">16</span> <span class="mi">22</span> <span class="mi">0</span> <span class="mi">18</span> <span class="mi">27</span> <span class="mi">1</span> <span class="mi">23</span> <span class="mi">25</span> <span class="mi">6</span> <span class="mi">13</span> <span class="mi">3</span> <span class="mi">8</span> <span class="mi">21</span> <span class="mi">17</span> <span class="mi">4</span> <span class="mi">11</span> <span class="mi">15</span> <span class="mi">26</span> 

<span class="o">*****</span>  <span class="n">the</span> <span class="n">sum</span> <span class="n">polynomials</span> <span class="n">of</span> <span class="n">one</span> <span class="n">indeterminate</span>  <span class="o">*****</span>
<span class="o">-</span><span class="mi">1</span><span class="n">x</span><span class="o">^</span><span class="mi">4</span><span class="o">+</span><span class="mi">4</span><span class="n">x</span><span class="o">^</span><span class="mi">3</span><span class="o">+</span><span class="mi">4</span><span class="n">x</span><span class="o">^</span><span class="mi">2</span><span class="o">+</span><span class="mi">3</span>

<span class="n">index</span>	<span class="n">data</span>	<span class="n">highpower</span>	<span class="n">next</span>
<span class="mi">26</span>		<span class="o">-</span><span class="mi">1</span>		<span class="mi">4</span>			<span class="mi">5</span>
<span class="mi">5</span>		<span class="mi">4</span>		<span class="mi">3</span>			<span class="mi">19</span>
<span class="mi">19</span>		<span class="mi">4</span>		<span class="mi">2</span>			<span class="mi">12</span>
<span class="mi">12</span>		<span class="mi">3</span>		<span class="mi">0</span>			<span class="o">-</span><span class="mi">1</span>

<span class="n">Stack</span><span class="p">(</span><span class="n">from</span> <span class="n">bottom</span> <span class="n">to</span> <span class="n">top</span><span class="p">)</span><span class="o">:</span><span class="mi">7</span> <span class="mi">24</span> <span class="mi">9</span> <span class="mi">20</span> <span class="mi">16</span> <span class="mi">22</span> <span class="mi">0</span> <span class="mi">18</span> <span class="mi">27</span> <span class="mi">1</span> <span class="mi">23</span> <span class="mi">25</span> <span class="mi">6</span> <span class="mi">13</span> <span class="mi">3</span> <span class="mi">8</span> <span class="mi">21</span> <span class="mi">17</span> <span class="mi">4</span> <span class="mi">11</span> <span class="mi">15</span> <span class="mi">29</span> <span class="mi">2</span> <span class="mi">10</span> <span class="mi">14</span> <span class="mi">28</span> 
<span class="n">Program</span> <span class="n">ended</span> <span class="n">with</span> <span class="n">exit</span> <span class="nl">code</span><span class="p">:</span> <span class="mi">0</span>
</code></pre></div>
</section>


    <footer class="article-footer">
    

    </footer>

    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"
    integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"
    integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4"
    crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js"
    integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous"
    onload="renderMathInElement(document.querySelector(`.article-content`));"></script>
    
</article>

    <aside class="related-contents--wrapper">
    
    
        <h2 class="section-title"></h2>
        <div class="related-contents">
            <div class="flex article-list--tile">
                
                    
<article class="has-image">
    <a href="https://hungyam.gitee.io/code/p/%E6%8B%AC%E5%8F%B7%E5%8C%B9%E9%85%8D/">
        
        
            <div class="article-image">
                <img src="/code/p/%E6%8B%AC%E5%8F%B7%E5%8C%B9%E9%85%8D/3_hud7e36f7e20e71be184458283bdae4646_55974_250x150_fill_q75_box_smart1.jpg" width="250" height="150" 
                        loading="lazy" data-key="" data-hash="md5-xRTZFpFxc6SKQuARS0aZYQ==">
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">括号匹配</h2>
        </div>
    </a>
</article>
                
                    
<article class="has-image">
    <a href="https://hungyam.gitee.io/code/p/%E4%B8%AD%E7%BC%80%E8%BD%AC%E5%90%8E%E7%BC%80%E5%90%8E%E7%BC%80%E6%B1%82%E8%A7%A3/">
        
        
            <div class="article-image">
                <img src="/code/p/%E4%B8%AD%E7%BC%80%E8%BD%AC%E5%90%8E%E7%BC%80%E5%90%8E%E7%BC%80%E6%B1%82%E8%A7%A3/3_hu0a3f1163de68d0b9471979ebf0ecf11e_32400_250x150_fill_q75_box_smart1.jpg" width="250" height="150" 
                        loading="lazy" data-key="" data-hash="md5-rC&#43;UhC7sa7h6Y66uIugvQQ==">
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">中缀转后缀、后缀求解</h2>
        </div>
    </a>
</article>
                
            </div>
        </div>
    
</aside>

    
        
    

    <footer class="site-footer">
    <section class="copyright">&copy; 2020 Hungyam</section>
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> <br />
        Theme <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="1.0.5">Stack</a></b> designed by
        <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a>
    </section>
</footer>
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true" style="display:none">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>
            </main>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.5/dist/vibrant.min.js"
    integrity="sha256-5NovOZc4iwiAWTYIFiIM7DxKUXKWvpVEuMEPLzcm5/g=" crossorigin="anonymous"></script><script type="text/javascript" src="/code/ts/main.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        document.head.appendChild(customFont);
    }());
</script>
<link rel="stylesheet" href="/code/css/highlight/light.min.css" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="/code/css/highlight/dark.min.css" media="(prefers-color-scheme: dark)">

    </body>
</html>
